<?php
/**
 * 核心Db框架
 * @author sunhaoqiang(shqdev@163.com)
 * @time: 2019-08-25 16:38
*/
namespace app\controller;

use app\item\Paper as PaperItem;
use app\item\Test as TestItem;
use app\item\Tizu as TizuItem;
use app\item\Question as QuestionItem;
use app\model\Paper as PaperModel;
use app\model\Base as BaseModel;

use script\Db;


class Que
{
    public function execute()
    {

        echo '<pre>';
        // $this->treeDir();
        // $this->load();

        $option = [
            'host' 		=> '127.0.0.1',
            'user' 		=> 'root',
            'pass' 		=> 'root',
            'dbname' 	=> 'spider',
            'port' 		=> 3306,
            'charset' 	=> 'utf8'
        ];
        // $db = Db::connect($option);

        $where = [
            ['id', '=', '136546']
        ];
        $res = Db::connect($option)->table('t_question')->where($where)->select();

        $option = $res[0]["options"];

        $str = html_entity_decode($option);

        $str = json_decode($str);
        
        var_dump($str);

        echo 'success';
    }

    public function treeDir($dir='./public/*')
    {
        $files = glob($dir);
        foreach ($files as $value) {
            if(is_dir($value)){
                $this->treeDir($value.'/*');
            }
            if(is_file($value)){
                if (pathinfo($value, PATHINFO_EXTENSION) == 'txt') {
                    $this->load($value);
                }
            }
        }
    }

    /**
     * paper-2-2-534.txt
     * 年级-科目-章节
     */
    public function load($file = './public/paper-2-2-534.txt')
    {
        
        $data = file_get_contents($file);
        $res = json_decode($data);


        $testItem = new TestItem();

        $tizuItem = new TizuItem();
        $questionItem = new QuestionItem();

        // 循环试卷列表
        foreach ($res as $key => $value) {
            
            if (isset($value->_meta)) {
                // 封装试卷数据
                $testItem->push((array)$value->_meta);

                // 循环题组列表
                foreach ($value->content as $k => $v) {
                    // 封装题组数据
                    $v->paper_id = $value->_meta->pid;
                    $tizu_item = $tizuItem->tizuSingle($v);
                    
                    $tizuItem->push($tizu_item);
                    

                    if (isset($v->scores)) {
                        $scores = [];
                        foreach ($v->scores as $k_s => $v_s) {
                            $scores[$k_s] = (array)$v_s;
                        }
                    }
                    foreach ($v->questions as $k_q => $v_q) {
                        // 二级题目处理
                        // tip: 分数
                        if (!empty($v_q->list)) {
                            $list = (array)$v_q->list;
                            $list = array_map([$questionItem ,'questionSingle'], $list);
                            $v_q->list = implode(',', array_column($list, 'question_id'));
                        }

                        if (isset($scores)) {
                            $v_q->score = isset($scores[$v_q->question_id]['score'])? $scores[$v_q->question_id]['score'] : '';
                            if (isset($scores[$v_q->question_id]['subScore'])) {
                                $v_q->subScore = is_array($scores[$v_q->question_id]['subScore'])? json_encode($scores[$v_q->question_id]['subScore']): $scores[$v_q->question_id]['subScore'];
                            }
                        }
                        $questionItem->questionSingle($v_q);
                    }
                }
                
            }else {
                echo "files-shq: ".$file."\n";
                var_dump($value);
            }            
        }

        
        $testItem->toDb();
        $tizuItem->toDb();
        $questionItem->toDb();

    }
    
}